import openpyxl
from openpyxl.styles import Font
from openpyxl.styles.colors import RED

CONFIG_XLS_FILE_PATH = "./../tableExport.xlsx"
CONFIG_PATCHED_ERROR_XLS = "/home/android/Opensource/WorkSpace_Py/Mtk_Patch/2018-04-16_13-23-26.xlsx"

if __name__ == '__main__':
    wb_old = None
    try:
        wb_old = openpyxl.load_workbook(filename=CONFIG_XLS_FILE_PATH, read_only=False)
    except Exception as e:
        print("open %s failed" % CONFIG_XLS_FILE_PATH, e)
        exit(-1)
    sheet_name = wb_old.sheetnames[0]
    print("sheet name %s" % sheet_name)
    sheet = wb_old[sheet_name]

    Patch_ID_list = sheet['C']

    wb_error = None
    try:
        wb_error = openpyxl.load_workbook(filename=CONFIG_PATCHED_ERROR_XLS, read_only=False)
    except Exception as e:
        print("open %s failed" % CONFIG_XLS_FILE_PATH, e)
        exit(-1)
    sheet_name_error = wb_error.sheetnames[0]
    print("sheet name %s" % sheet_name_error)
    sheet_error = wb_error[sheet_name_error]

    Title_list = sheet_error['B']
    Result_list = sheet_error['I']

    patched_count = 0
    for index in range(len(Title_list)):
        if Result_list[index].value is not None and Result_list[index].value.encode("utf-8") == 'Failed':
            title_error = Title_list[index].value.encode("utf-8")
            for index_old in range(len(Patch_ID_list)):
                patch_id = Patch_ID_list[index_old]
                if patch_id is not None:
                    patch_id = patch_id.value
                    if patch_id is not None:
                        patch_id = patch_id.encode("utf-8")
                        if patch_id in title_error:
                            patched_count = patched_count + 1
                            Title_list[index].font = Font(size=12, color=RED)
                            print("found %s already patched at %s" % (title_error, patch_id))
    wb_error.save(CONFIG_PATCHED_ERROR_XLS)
    print("patched count %s" % patched_count)
